var NationalPortalConfig = {
	columns: [
		{
			width: 0.6, 
			style: "padding:10px 0 10px 10px",
			portlets: [
				{
					title: "portal1",
					height: 300,
					max: false,
					xtype: "panel"
				},
				{
					title: "portal2",
					height: 300,
					max: false,
					xtype: "panel"
				}
			]
		},
		{
			width: 0.4,
			style: "padding:10px",
			portlets: [
				{
					title: "portal3",
					height: 300,
					max: false,
					xtype: "panel"
				},
				{
					title: "portal4",
					height: 300,
					max: false,
					xtype: "lastUpdatedDocPortlet"
				}
			]
		}
	]
};

var ProvincialPortalConfig = {
	columns: [
		{
			width: 0.6,
			style: "padding:10px 0 10px 10px",
			portlets: [
				{
					title: "portal1",
					height: 300,
					max: true,
					xtype: "panel"
				},
				{
					title: "portal2",
					height: 300,
					max: true,
					xtype: "panel"
				}
			]
		},
		{
			width: 0.4,
			style: "padding:10px",
			portlets: [
				{
					title: "portal3",
					height: 300,
					max: false,
					xtype: "panel"
				},
				{
					title: "portal4",
					height: 300,
					max: false,
					xtype: "lastUpdatedDocPortlet"
				}
			]
		}
	]
}


/**
 * 最新项目资料
 * @class LastUpdatedDocumentPanel
 * @extends Ext.Panel
 */
LastUpdatedDocumentPanel = Ext.extend(Ext.Panel, {
	id: 'lastUpdatedDocs',
    autoHeight: true,
    region: 'center',
    store: null,
    statTpl: null,
    html: '<div style="padding-top:20px;text-align:center">目前没有任何文档资料。</div>',
    
    initComponent: function() {
		this.store = new Ext.data.JsonStore({
			root: 'resultSet',
			idProperty: 'threadid',
			fields: ['threadid', 'name'],
		    proxy: new Ext.data.HttpProxy({
		    	 url: 'document/lastUpdateDocuments.do',
		    	 method: 'GET'
		    }),
		    autoLoad: false
		});  
		this.statTpl = new Ext.XTemplate(
			'<tpl for=".">',
        		'<div style="padding: 4px; border-bottom:1px solid #EDEDED;white-space:nowrap;text-overflow: ellipsis;font-size:12px">',
					'<div>', //ext:qtip="{time}"
						'<img src="images/arrow.gif" style="padding-right:10px; " />',
						'<a href="javascript:showViewDocumentMgmtPanel({threadid});">{name}</a>', 
					'</div>',
				'</div>',
			'</tpl>', 
			'<div style="padding:6px;">',
				'<div style="text-align: right; border: 0;"><a href="javascript:showDocumentMgmtPanel();">查看更多>></a></div>',
			'</div>'
        );
		LastUpdatedDocumentPanel.superclass.initComponent.call(this);
	},
	
	loadData : function() {
    	this.store.load({
    		params:{start:0, limit:10},
    		scope: this,
    		callback: function(records, options, success) {
    			if(records != null && records.length > 0) {
	    			var dataValues = [];
	    			for(var i=0;i<records.length;i++) {
	    				dataValues.push(records[i].data);
	    			}
	    			this.statTpl.overwrite(this.body, dataValues);
    			}
    		}
    	});
    }
});

Ext.reg('lastUpdatedDocPortlet', LastUpdatedDocumentPanel);


/**
 * 工作台面板
 * @class WorkbenchPanel
 * @extends Ext.Panel
 */
WorkbenchPanel = Ext.extend(Ext.Panel, {
	
	portalContainer: null,
	  
	constructor: function(_config) {
		if (_config == null) {
			_config = {};
		}
		
		Ext.apply(this, _config);
		
		WorkbenchPanel.superclass.constructor.call(this, {
			id : 'workbench',
			//title: '我的工作台',
			autoScroll: true
		});
	},
	
	initComponent: function() {
		WorkbenchPanel.superclass.initComponent.call(this);
	},
	
	init: function() {
		//从服务器获取
		var _config =  this.getState();
		
		this.portalContainer = new Ext.ux.Portal({
			margins:'35 5 5 0',
			border: false,
			portalConfig: _config
		});
		
		this.portalContainer.on("drop", this.saveState, this);
		
		this.add(this.portalContainer);
		this.doLayout();
		
		this.portalContainer.initPortal();
	},
	
	getState: function() {
		var userConfig  = NationalPortalConfig;
		Ext.Ajax.request({url: 'getWorkspace.do', method: 'GET', scope: this, async: false,
    		success: function(response, options) {
    			var result = Ext.util.JSON.decode(response.responseText);
    			if(result.success) {
    				if(!Ext.isEmpty(result.userConfig)) {
    					userConfig = Ext.util.JSON.decode(result.userConfig);
    				} else {
    					if(!Ext.isEmpty(result.configType)) {
    						if(result.configType == 1) {
    							userConfig = NationalPortalConfig;
    						} else if(result.configType == 2) {
    							userConfig = ProvincialPortalConfig;
    						}
    					} else {
    						userConfig = NationalPortalConfig;
    					}
    				}
    			} else {
    				Ext.MessageBox.alert("系统提示：", result.error);
    			}
    		}
    	});
    	return userConfig;
	},
	
	saveState: function(dropEvent) {
		this.portalContainer.dropPortlet(dropEvent);
		this.portalContainer.saveState();
	},
	
	refresh: function() {
		Ext.TaskMgr.stopAll();
		Ext.TaskMgr.start(global_task);
	}
});

function handleMaximize(event, toolEl, panel) {
	//获取column面板
	var columnPanel = panel.ownerCt;
	//获取portal面板
	var portalPanel = columnPanel.ownerCt;
	
	portalPanel.maximizePortlet(panel);
	portalPanel.saveState();
	
	Ext.TaskMgr.stopAll();
	Ext.TaskMgr.start(global_task);
};

function handleMinimize(event, toolEl, panel) {
	//获取column面板
	var columnPanel = panel.ownerCt;
	//获取portal面板
	var portalPanel = columnPanel.ownerCt;
	
	portalPanel.minimizePortlet(panel);
	portalPanel.saveState();
	
	Ext.TaskMgr.stopAll();
	Ext.TaskMgr.start(global_task);
}
